package interfaces.loading;

import java.util.Collection;

import main.GameCycle;
import map.Map;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.util.Color;

import fileHandling.language.LanguageLoader;
import gameStates.absGamesStates.gui.AbsLoadingState;
import interfaces.GUISource;
import interfaces.ImageUpdater;
import interfaces.PropertyContent;
import interfaces.WordWrapHelper;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.StaticContent;

public class MapLoadingScreen extends LoadingScreen {
	
	private MapInfoWindow infoWindow;
	private GameModeWindow gameModeWindow;
	private StaticContent hintContent;
	private int border;
	private Map map;
	private GameCycle gameCycle;
	
	public MapLoadingScreen(AbsLoadingState loadingState, Map map, GameCycle gameCycle) {
		super(loadingState);
		this.map = map;
		this.gameCycle = gameCycle;
		
		border = loadingState.getHeight() / 28;
		
		createBackground();
		createLoadingBar();
		createHintContent();
		createInfoWindow();
		createGameModeWindow();
	}
	
	private void createHintContent() {
		ImageFont font = GUISource.middleFont;
		int width = getWidth() - (2 * border);
		int height = font.getHeight();
		int x = border;
		int y = loadingContent.getY() + loadingContent.getHeight() + border;
		
		hintContent = new StaticContent(width, height, x, y);
		hintContent.getAppearance().add(new PlainBackground(Color.BLUE));
		addWidget(hintContent);
		
		String hintText = LanguageLoader.getRandomHint();
		InterfaceLabel hintlabel = new InterfaceLabel(font);
		hintlabel.setSize(width, height);
		
		Collection<InterfaceLabel> labels = WordWrapHelper.getCentricLabels(hintlabel, hintText);
		int contentHeight = height * labels.size();
		
		hintContent.setHeight(contentHeight);
		
		for(InterfaceLabel label : labels) {
			GUISource.setTextColor(label, Color.WHITE);
			hintContent.addWidget(label);
		}
		
		hintContent.layout();
	}
	
	private void createLoadingBar() {
		int width = getWidth() - (2 * border);
		int height = getHeight() / 40;
		
		loadingContent = new PropertyContent(width, height, border, border);
		loadingContent.setMaxValue(100f);
		loadingContent.setValue(0f);
		loadingContent.changeBackgroundColor(Color.BLUE);
		loadingContent.changeValueColor(Color.BLACK);
		addWidget(loadingContent);
	}
	
	private void createBackground() {
		String previewPath = map.getPreviewPath();
		ImageUpdater background = new ImageUpdater(previewPath, 0, 0, getWidth(), getHeight());
		addWidget(background);
	}
	
	private void createGameModeWindow() {
		int x = border;
		int y = infoWindow.getY() + infoWindow.getHeight() + border;
		int height = getHeight() - y - border;
		int width = getWidth() / 3;
		
		String mode = LanguageLoader.get(gameCycle.getGameMode().getGameModeOptionValue());
		gameModeWindow = new GameModeWindow(map, gameCycle, mode, width, height);
		gameModeWindow.setXY(x, y);
		addWidget(gameModeWindow);
	}
	
	private void createInfoWindow() {
		int x = border;
		int y = hintContent.getHeight() + hintContent.getY() + border;
		int height = getHeight() - (border * 2) - y - (getHeight() / 4);
		int width = height / 2 * 3;
		
		infoWindow = new MapInfoWindow(map, gameCycle, width, height);
		infoWindow.setXY(x, y);
		addWidget(infoWindow);
	}
}